package com.woniu.dating.controller;

import com.woniu.common.annotation.LoginDating;
import com.woniu.common.annotation.LoginUser;
import com.woniu.common.constant.RedisKeyConstant;
import com.woniu.common.entity.Result;
import com.woniu.common.entity.ResultCode;
import com.woniu.common.utils.StringUtils;
import com.woniu.dating.dto.DatingHobbyDTO;
import com.woniu.dating.pojo.DatingHobby;
import com.woniu.dating.pojo.DatingInfo;
import com.woniu.dating.service.IDatingHobbyService;
import com.woniu.dating.service.IDatingInfoService;
import com.woniu.dating.vo.DatingLoginVO;
import com.woniu.dating.vo.RecommendVO;
import com.woniu.user.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 玖柒
 * @since 2023-05-20
 */
@RestController
@RequestMapping("/datingHobby")
@Api(tags = "相亲兴趣收集")
public class DatingHobbyController {


    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IDatingHobbyService datingHobbyService;

    @Resource
    private IDatingInfoService datingInfoService;


    /**
     * 首次进入自动创建默认用户信息
     *
     */
    @ApiOperation("自动创建用户兴趣信息")
    @GetMapping("create")
    public Result createHobby(@LoginUser UserLoginVO userLoginVO) {
        //1.判断redis中是否存在创建key，存在就不创建，不存在就创建
        Integer key = (Integer) redisTemplate.opsForValue().get(RedisKeyConstant.DATING_CREATE+userLoginVO.getId());
        //2.如果存在，就返回状态码30000,表示用户已创建，如果不存在就创建默认对象
        if (key != null) {
            return Result.ok(ResultCode.DATING_CREATE);
        }
        //3.创建默认对象
        datingHobbyService.createHobby(userLoginVO);
        //4.返回成功，状态码
        return Result.ok(ResultCode.DATING_SUCCESS);
    }

    /**
     *
     * 用户第一次登录，需要填写兴趣信息
     */
    @ApiOperation("用户提交创建信息")
    @PostMapping("/create/hobby")
    public Result createFirstInfo(@RequestBody DatingHobbyDTO datingHobbyDTO, @LoginUser UserLoginVO userLoginVO) {
        //1.因为已经有默认信息了，先判断默认信息是否存在，再进行修改或新增。
        DatingHobby getDatingHobby = datingHobbyService.getDatingHobbyById(userLoginVO.getId());
        if (getDatingHobby == null) {
            datingHobbyService.createHobby(userLoginVO);
        }
        //2.合并对象
        BeanUtils.copyProperties(datingHobbyDTO,getDatingHobby);
        //3.修改已存在的信息
        datingHobbyService.updateById(getDatingHobby);
        return Result.ok();
    }

    /**
     * 根据用户id,查询用户相亲信息
     */
    @ApiOperation("根据用户id,查询相亲兴趣信息")
    @GetMapping("/get/id/{userId}")
    public Result<DatingHobby> getDatingHobbyById(@PathVariable Integer userId) {
        DatingHobby datingHobby = datingHobbyService.getDatingHobbyById(userId);
        return Result.ok(datingHobby);
    }

    /**
     * 推荐关注度从高到低的用户
     */
    @ApiOperation("推荐信息")
    @GetMapping("/recommend")
    public Result<List<RecommendVO>> listRecommendInfo() {
        //1.查询出关注度从高到低的人员信息
        List<RecommendVO> listRecommend = datingHobbyService.listRecommendInfo();
        return Result.ok(listRecommend);
     }

    /**
     * 同乡录，查出登录用户异性的所有人员，按关注度排序
     */
    @ApiOperation("同乡录")
    @GetMapping("/fellow")
    public Result<List<RecommendVO>> listFellow(@LoginDating DatingLoginVO datingLoginVO) {
        System.out.println(datingLoginVO.getUserId());
        //1.查询出所有同乡的人，按照关注度排序
        List<RecommendVO> listRecommendVO = datingHobbyService.listFellow(datingLoginVO);
        return Result.ok(listRecommendVO);
    }

    /**
     * 找男友
     */
    @ApiOperation("找男友")
    @GetMapping("/boy")
    public Result<List<RecommendVO>> listBoy() {
        //1.查询出相亲信息表中所有男性的信息，按关注度排序
        List<RecommendVO> listRecommendVO = datingHobbyService.listBoy();
        return Result.ok(listRecommendVO);
    }

    /**
     * 找男友
     */
    @ApiOperation("找女友")
    @GetMapping("/girl")
    public Result<List<RecommendVO>> listGirl() {
        //1.查询出相亲信息表中所有男性的信息，按关注度排序
        List<RecommendVO> listRecommendVO = datingHobbyService.listGirl();
        return Result.ok(listRecommendVO);
    }

    /**
     *信息展示，根据用户id,展示数据
     */
    @ApiOperation("查询用户完整信息")
    @GetMapping("/user/{userId}")
    public Result<RecommendVO> getUserDatingHobby(@LoginDating DatingLoginVO datingLoginVO) {
        RecommendVO recommendVO = datingHobbyService.getUserDatingHobby(datingLoginVO.getUserId());
        return Result.ok(recommendVO);
    }

    /**
     * 根据手机号获取用户
     */
    @ApiOperation("手机号获取用户")
    @GetMapping("/mobile/{mobile}")
    public Result<DatingLoginVO> getDatingHobbyByMobile(@PathVariable String mobile) {
        DatingLoginVO datingLoginVO = datingHobbyService.getDatingHobbyByMobile(mobile);
        return Result.ok(datingLoginVO);
    }
}
